package live.yanxiaohui.deadletter.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description todo
 * @CSDN https://blog.csdn.net/yxh13521338301
 * @Author： yanxh<br>
 * @Date 2020-06-29 09:45<br>
 * @Version 1.0<br>
 */
@Configuration
public class DeadLetterMQConfig {

    /**
     * 死信队列名称
     */
    @Value("${yxh.dlx.queue}")
    private String dlxQueue;

    /**
     * 死信交换机名称
     */
    @Value("${yxh.dlx.exchange}")
    private String dlxExchange;

    /**
     * 死信路由名称
     */
    @Value("${yxh.dlx.routingKey}")
    private String dlxRoutingKey;


    /**
     * 订单队列名称
     */
    @Value("${yxh.order.queue}")
    private String orderQueue;

    /**
     * 订单交换机名称
     */
    @Value("${yxh.order.exchange}")
    private String orderExchange;

    /**
     * 订单路由名称
     */
    @Value("${yxh.order.routingKey}")
    private String orderRoutingKey;


    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue dlxQueue(){
        return new Queue(dlxQueue);
    }

    /**
     * 死信扇形交换机
     * @return
     */
    @Bean
    public FanoutExchange dlxFanoutExchange(){
        return new FanoutExchange(dlxExchange);
    }

    /**
     * 死信队列绑定至死信交换机
     * @param dlxQueue
     * @param dlxFanoutExchange
     * @return
     */
    @Bean
    public Binding dxlFanoutBinding(Queue dlxQueue, FanoutExchange dlxFanoutExchange){
        return BindingBuilder.bind(dlxQueue).to(dlxFanoutExchange);
    }


    /**
     * 订单队列
     * @return
     */
    @Bean
    public Queue orderQueue(){
        // 订单队列绑定我们的死信交换机
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", dlxExchange);
        arguments.put("x-dead-letter-routing-key", dlxRoutingKey);
        return new Queue(orderQueue, true, false, false, arguments);
    }

    /**
     * 订单扇形交换机
     * @return
     */
    @Bean
    public FanoutExchange orderFanoutExchange(){
        return new FanoutExchange(orderExchange);
    }


    /**
     * 订单队列绑定至订单交换机
     * @param orderQueue
     * @param orderFanoutExchange
     * @return
     */
    @Bean
    public Binding orderFanoutBinding(Queue orderQueue, FanoutExchange orderFanoutExchange){
        return BindingBuilder.bind(orderQueue).to(orderFanoutExchange);
    }
}

